package com.atguigu.servicesta.service.impl;

import com.atguigu.servicesta.client.UcenterClient;
import com.atguigu.servicesta.entity.StatisticsDaily;
import com.atguigu.servicesta.mapper.StatisticsDailyMapper;
import com.atguigu.servicesta.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author JY
 * @since 2021-02-07
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void createStatisticsByDate(String day) {
        //删除已存在的统计对象
        QueryWrapper<StatisticsDaily> wrapper =new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        //获取统计信息
        Integer registerNum = (Integer) ucenterClient.countRegister(day).getData().get("countRegister");
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        baseMapper.insert(daily);
    }

    @Override
    public Map<String, Object> showChart(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select(type, "date_calculated");
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);

        Map<String, Object> map = new HashMap<>();
        //定义两个list用于返回图表需要的时间和数量数据
        List<Integer> numList = new ArrayList<>();
        List<String> dateList = new ArrayList<>();

        for (StatisticsDaily statisticsDaily : statisticsDailies) {
            dateList.add(statisticsDaily.getDateCalculated());
            switch (type) {
                case "register_num":
                    numList.add(statisticsDaily.getRegisterNum());
                    break;
                case "login_num":
                    numList.add(statisticsDaily.getLoginNum());
                    break;
                case "video_view_num":
                    numList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        map.put("dataList", numList);
        map.put("dateList", dateList);
        return map;
    }
}
